package cn.zjxiliu.lib_login.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import cn.zjxiliu.lib_core.widget.ClearEditText;
import cn.zjxiliu.lib_login.R;

/**
 * Created by jessie on 2018/11/21.
 */

public class PasswordInputView extends LinearLayout implements View.OnClickListener {

    private LinearLayout inputPasswordView;

    private ClearEditText etPassword;
    private View viewLine;
    private ImageView ivSeePassword;

    private InputViewCallbackListener listener;

    //是否隐藏密码
    private boolean currentShowPassword = false;


    public void setListener(InputViewCallbackListener listener) {
        this.listener = listener;
    }

    public PasswordInputView(Context context) {
        super(context);
    }

    public PasswordInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inputPasswordView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.input_password_view, null);

        etPassword = (ClearEditText) inputPasswordView.findViewById(R.id.etPassword);
        viewLine = inputPasswordView.findViewById(R.id.viewLine);

        ivSeePassword = (ImageView) inputPasswordView.findViewById(R.id.ivSeePassword);

        etPassword.setLongClickable(false);
        etPassword.setTextIsSelectable(false);
        initClick();
        initListen();
        addView(inputPasswordView);
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.ivSeePassword) {
            currentShowPassword = !currentShowPassword;
            if (currentShowPassword) {
                ivSeePassword.setImageResource(R.drawable.login_icon_regist_password_see_not_selected);
                etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

            } else {
                //设置密码为不可见
                ivSeePassword.setImageResource(R.drawable.login_icon_regist_password_see_selected);
                etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            if(!TextUtils.isEmpty(getText())){
                etPassword.setSelection(getText().length());
            }

        }
    }

    private void initClick() {
        ivSeePassword.setOnClickListener(this);
    }

    private void initListen() {
        etPassword.setTextChangeListener(new ClearEditText.OnTextChangeListener() {
            @Override
            public void onFocusChange(boolean hasFocus) {
                if (hasFocus) {
                    viewLine.setBackgroundColor(getResources().getColor(R.color.c3F86FF));
                    if (listener != null) {
                        listener.onFocusChange(hasFocus);
                    }
                } else {
                    viewLine.setBackgroundColor(getResources().getColor(R.color.cDBDBDB));
                    if (listener != null) {
                        listener.onFocusChange(hasFocus);
                    }
                }
            }

            @Override
            public void afterTextChanged(String s) {
                if (TextUtils.isEmpty(s)) {
                    if (listener != null) {
                        listener.inputViewHasText(false);
                    }
                } else {
                    if (listener != null) {
                        listener.inputViewHasText(true);
                    }
                }
            }
        });
    }

    public String getText() {
        return etPassword.getText().toString();
    }


    public PasswordInputView setHintText(String text) {
        etPassword.setHint(text);
        return this;
    }

    public PasswordInputView setInputType(int type) {
        etPassword.setInputType(type);
        return this;
    }

    public PasswordInputView isHideUnderline(boolean isHide) {
        if (isHide) {
            viewLine.setVisibility(GONE);
        } else {
            viewLine.setVisibility(VISIBLE);
        }
        return this;
    }

    public PasswordInputView setHintColor(int color) {
        etPassword.setHintTextColor(color);
        return this;
    }

    public PasswordInputView setTextSize(float size) {
        etPassword.setTextSize(size);
        return this;
    }

    public PasswordInputView setTextColor(int color) {
        etPassword.setTextColor(color);
        return this;
    }

    public PasswordInputView setMaxLength(int maxLength) {
        etPassword.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });
        return this;
    }

    public PasswordInputView setDrawableLeft(Drawable drawableLeft) {
        etPassword.setDrawableLeft(drawableLeft);
        return this;
    }
}
